// component/index-goods-item/index-goods-item.js
const app = getApp();
const api = require("../../server/Api.js")
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    goods: Object,
    fromPage: String,
    type: Number
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    lower_count: function() {
      this.doCart(parseInt(this.data.goods.goodsQuantity) - 1, -1);
    },
    upper_count: function() {
      this.doCart(this.data.goods.goodsQuantity ? (parseInt(this.data.goods.goodsQuantity) + 1) : 1, 1);
    },
    doCart: function(goodsCount, num) {
      if (this.data.goods.receipt_interval != 1) {
        //app.showToast("不是隔天可提货的商品暂不支持加入购物车,请直接购买")
        app.showToast("该商品不支持加上购物车")
        return;
      }
      let userBean = app.globalData.userBean;
      if (!userBean || !userBean.consumerId) {
        tt.navigateTo({
          url: '../login/login',
        })
        return;
      }

      let that = this;
      //服务器加入购物车
      console.log(this.data.goods);
      let skuId = this.data.goods.sku_id || this.data.goods.sku_Id;
      let userId = app.globalData.userBean.consumerId;
      let recId = this.data.goods.recId;
      if (goodsCount === 0 && recId) {
        api.deleteCart(recId).then(res => {
          that.data.goods.recId = undefined;
          that.data.goods.goodsQuantity = goodsCount;
          that.setData({
            goods: that.data.goods
          });
          this.setPageUpdate(skuId, num);
        }).catch(msg => {
          let message = typeof msg == "string" ? msg : msg.message;
          app.showToast(message)
        })
      } else if (goodsCount === 1 && num === 1) {
        api.addCart(goodsCount, skuId, userId).then(res => {
          that.data.goods.recId = res.recId;
          that.data.goods.goodsQuantity = goodsCount;
          that.setData({
            goods: that.data.goods
          });
          this.setPageUpdate(skuId, num);
        }).catch(msg => {
          let message = typeof msg == "string" ? msg : msg.message;
          app.showToast(message)
        })
      } else {
        api.updateCartCount(recId, goodsCount).then(res => {
          that.data.goods.goodsQuantity = goodsCount;
          that.setData({
            goods: that.data.goods
          });
          this.setPageUpdate(skuId, num);
        }).catch(msg => {
          let message = typeof msg == "string" ? msg : msg.message;
          app.showToast(message);
        })
      }
    },
    setPageUpdate(skuid, num, goodsQuantity) {
      if (this.data.fromPage === "index") {
        app.globalData.cartUpdated = false;
      } else if (this.data.fromPage === "cart") {
        app.globalData.indexCartUpdated = false;
      }
      //let goodsQuantityAll = app.globalData.cartGoodsNum + num
      // if (goodsQuantityAll > 0) {
      //   tt.setTabBarBadge({
      //     index: 1,
      //     text: '' + goodsQuantityAll
      //   })
      // } else {
      //   tt.removeTabBarBadge({
      //     index: 1,
      //   })
      // }
      //app.globalData.cartGoodsNum = goodsQuantityAll;
      this.triggerEvent('refreshCart', {
        num: num,
        skuId: skuid,
        goods: this.data.goods
      })
    },
    goDetail: function(e) {
      if (this.data.goods.id)
        tt.navigateTo({
          url: '../goods-detail/goods-detail?id=' + this.data.goods.id,
        })
    },
    toBuyRecord: function() {
      tt.navigateTo({
        url: '../buy-record/buy-record?id=' + this.data.goods.id,
      })
    }
  }
})